import com.cult3d.world.*;
import com.cult3d.*;

import java.awt.Point;

/**
 * This code demonstrates how to test which node are under the current mouse position
 * It works with Cult3D 5.3 and later
 */
public class GetTree implements Cult3DScript, Runnable
{
	private static final int SLEEP_TIME = 18;
	
	private volatile boolean stillGoing = true;
	
	private CultObject currentlySelectedObject;
	
	public GetTree()
	{
		System.out.println("Java Started");
		
		new Thread(this).start();
	}
	
	private String ConvertToUnicode(String str)
	{
		String ret="UNICODE";
		for(int i=0;i<str.length();i++)
		{
			//if(str.charAt(i)<=255)ret+="00";
			ret+=Integer.toHexString(str.charAt(i));
		}
		return ret;
	}
	
	
	private void FirstViewTree(Node node,String perfix)
	{
		if(node==null) return;
		Cult.sendMessageToHost(ConvertToUnicode(perfix+node.getName()));
		for(int i=0;i<node.getChildCount();i++)
		{
			FirstViewTree(node.getChildAt(i),perfix+"  ");
		}
	}
	
	public void run()
	{
		System.out.println("Thread started");

		Node root=Node.getFirstNode().getNextSibling();
		while(root!=null)
		{
			if(root.getInfo().charAt(18)=='o' || root.getInfo().charAt(18)=='u')
				FirstViewTree(root,"");
			root=root.getNextSibling();
		}
		
		System.out.println("Thread ended");
	}
	
	public void ShowThisPart(String partName)
	{
		Node part=FindNode(partName);
		if(part==null) return;
		HideAllNode();
		ShowNode(part);
	}
	
	private void ShowNode(Node node)
	{
		if(node==null) return;
		node.setVisible(true);
		for(int i=0;i<node.getChildCount();i++)
		{
			ShowNode(node.getChildAt(i));
		}
	}
	
	private void HideNode(Node node)
	{
		if(node==null) return;
		node.setVisible(false);
		for(int i=0;i<node.getChildCount();i++)
		{
			HideNode(node.getChildAt(i));
		}

	}
	
	private void HideAllNode()
	{
		Node root=Node.getFirstNode().getNextSibling();
		while(root!=null)
		{
			if(root.getInfo().charAt(18)=='o' || root.getInfo().charAt(18)=='u')
				HideNode(root);
			root=root.getNextSibling();
		}
	}
	
	private Node FindNodeSons(Node node,String target)
	{
		if(node==null) return null;
		if(node.getName().compareTo(target)==0) return node;
		for(int i=0;i<node.getChildCount();i++)
		{
			Node tempNode=FindNodeSons(node.getChildAt(i),target);
			if(tempNode==null) continue;
			if(tempNode.getName().compareTo(target)==0)
			{
				return tempNode;
			}
		}
		return null;
	}
	
	private Node FindNode(String nodeName)
	{
		Node root=Node.getFirstNode().getNextSibling();
		Node tempNode=null;
		while(root!=null)
		{
			if(root.getInfo().charAt(18)=='o' || root.getInfo().charAt(18)=='u')
				tempNode=FindNodeSons(root,nodeName);
			if(tempNode!=null && 
			tempNode.getName().compareTo(nodeName)==0) 
				return tempNode;
			root=root.getNextSibling();
		}
		return null;
	}
	
	public void cult3dDestroy()
	{
		System.out.println("Entererd cult3dDestroy()");
		stillGoing = false;
	}
}
